import pygame

from pygame.sprite import Group
from setting import Settings
from ship import Ship
from alien import Alien
import game_functions as gf
from button import Button
from game_stats import GameStats
from scoreboard import Scoreboard

def run_game():
     pygame.init()
     ai_settings = Settings()
     screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
     pygame.display.set_caption("Alien Invasion")

     #創建play按鈕
     play_button = Button(ai_settings,screen,"Play")
     
     #創建一個用於存儲遊戲統計信息的實例,m并創建記分牌
     stats = GameStats(ai_settings)
     sb = Scoreboard(ai_settings,screen,stats)
     
     
     #創建一艘飛船
     ship = Ship(ai_settings,screen)
     #創建一個用於存儲子彈的編組
     bullets = Group()
     #創建一個外星人編組
     aliens = Group()

     #創建外星人群
     gf.create_fleet(ai_settings,screen,ship,aliens)
     
     screen.fill(ai_settings.bg_color)
     #創建一個外星人
     alien = Alien(ai_settings,screen)
     
     
     #開始遊戲循環
     while True:        
        gf.check_events(ai_settings,screen,stats,play_button,ship,aliens,bullets)
        if stats.game_active:
           ship.update()
           gf.update_bullets(ai_settings,screen,ship,aliens,bullets)
           gf.update_aliens(ai_settings,stats,screen,ship,aliens,bullets)
        gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)   
run_game()
